package com.xnewtech.xplatform.swan.model.page;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 封装分页结果集
 *
 * @author xnew
 * @Date 2018/7/22 23:00
 */
@Data
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = -4071521319254024213L;

    /**
     * 要查找第几页
     */
    private Integer page = 1;

    /**
     * 每页显示多少条
     */
    private Integer pageSize = 20;

    /**
     * 总页数
     */
    private Integer totalPage = 0;

    /**
     * 总记录数
     */
    private Long totalRows = 0L;

    /**
     * 结果集
     */
    private List<T> rows;

    public PageResult() {
    }

    public PageResult(IPage<T> page) {
        this.setPage(Convert.toInt(page.getCurrent()));
        this.setPageSize(Convert.toInt(page.getSize()));
        this.setTotalPage(PageUtil.totalPage(Convert.toInt(page.getTotal()), Convert.toInt(page.getSize())));
        this.setTotalRows(page.getTotal());
        this.setRows(page.getRecords());
    }

}
